package com.winning.vsts.merge;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.winning.vsts.merge.service.configuration.VstsContext;
import com.winning.vsts.merge.service.domain.VstsProject;
import com.winning.vsts.merge.service.service.RecordPullRequestService;
import com.winning.vsts.merge.service.service.VstsClient;
import com.winning.vsts.merge.service.service.VstsProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 自动合并
 *
 * @author xcl <xcl@winning.com.cn>
 * @date 2021/11/17
 */
@EnableJpaRepositories(basePackages = "com.winning.vsts.merge.service.jpa")
@SpringBootApplication(scanBasePackages = "com.winning.vsts.merge.service")
public class AutoMergeApplication implements ApplicationRunner {

    private final static Logger log = LoggerFactory.getLogger(AutoMergeApplication.class);

    @Autowired
    private VstsProjectService vstsProjectService;

    @Autowired
    private VstsClient vstsClient;

    @Autowired
    private VstsContext vstsContext;

    @Autowired
    private RecordPullRequestService recordPullRequestService;


    public static void main(String[] args) {
        new SpringApplicationBuilder()
            .main(AutoMergeApplication.class)
            .sources(AutoMergeApplication.class)
            .web(WebApplicationType.NONE)
            .build().run(args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Boolean queryStatus = false;
        if(!queryStatus){
            String sourceBranch = "develop";
            String targetBranch = "RC";
            String pullNo = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
            log.info("===============================开启批次{}自动合并===============================", pullNo);
            List<VstsProject> vstsProjects = vstsProjectService.queryVstsProject(null, null);
            for (VstsProject vstsProject : vstsProjects) {
                try {
                    //if(!vstsProject.getProjectName().contains("winning-web-outpatient-office")) continue;
                    RestTemplate restTemplate = vstsContext.restTemplate(vstsProject);
                    vstsClient.getRepository(restTemplate, vstsProject);
               /* Boolean tagFlag =vstsClient.checkLastCommitHaveTag(restTemplate,vstsProject,targetBranch);
                if(!tagFlag){
                    log.error("===============================项目{}合并失败，请先打好标签===============================", vstsProject.getProjectName());
                    continue;
                }*/
                    recordPullRequestService.createPullRequest(vstsProject, sourceBranch, targetBranch, pullNo);
                } catch (Exception e) {
                    log.warn("项目:{}/{}没有权限", vstsProject.getProjectOrgan(), vstsProject.getProjectName());
                    log.error(e.getMessage(), e);
                }
            }
            log.info("===============================批次{}自动合并完成===============================", pullNo);
        }
        recordPullRequestService.queryPullRequestStatus();
    }
}
